<template>
  <el-dialog
    :title="!dataForm.patientId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="昵称" prop="patientPackname">
      <el-input v-model="dataForm.patientPackname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="patientPwd">
      <el-input v-model="dataForm.patientPwd" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="患者姓名" prop="patientName">
      <el-input v-model="dataForm.patientName" placeholder="患者姓名"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="patientImg">
      <el-input v-model="dataForm.patientImg" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="patientBalance">
      <el-input v-model="dataForm.patientBalance" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="patientIntegral">
      <el-input v-model="dataForm.patientIntegral" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="patientAddress">
      <el-input v-model="dataForm.patientAddress" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="patientTel">
      <el-input v-model="dataForm.patientTel" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="出生日期" prop="patientBirth">
      <el-input v-model="dataForm.patientBirth" placeholder="出生日期"></el-input>
    </el-form-item>
    <el-form-item label="年龄" prop="patientAge">
      <el-input v-model="dataForm.patientAge" placeholder="年龄"></el-input>
    </el-form-item>
    <el-form-item label="体重" prop="patientWeight">
      <el-input v-model="dataForm.patientWeight" placeholder="体重"></el-input>
    </el-form-item>
    <el-form-item label="身高" prop="patientHight">
      <el-input v-model="dataForm.patientHight" placeholder="身高"></el-input>
    </el-form-item>
    <el-form-item label="肾功能" prop="patientShen">
      <el-input v-model="dataForm.patientShen" placeholder="肾功能"></el-input>
    </el-form-item>
    <el-form-item label="肝功能" prop="patientGan">
      <el-input v-model="dataForm.patientGan" placeholder="肝功能"></el-input>
    </el-form-item>
    <el-form-item label="生育状态" prop="patientFertility">
      <el-input v-model="dataForm.patientFertility" placeholder="生育状态"></el-input>
    </el-form-item>
    <el-form-item label="疾病史" prop="patientHistory">
      <el-input v-model="dataForm.patientHistory" placeholder="疾病史"></el-input>
    </el-form-item>
    <el-form-item label="问诊次数" prop="patientNum">
      <el-input v-model="dataForm.patientNum" placeholder="问诊次数"></el-input>
    </el-form-item>
    <el-form-item label="支付总额" prop="patientMoney">
      <el-input v-model="dataForm.patientMoney" placeholder="支付总额"></el-input>
    </el-form-item>
    <el-form-item label="患者性别" prop="patientSex">
      <el-input v-model="dataForm.patientSex" placeholder="患者性别"></el-input>
    </el-form-item>
    <el-form-item label="身份证号" prop="patientCardno">
      <el-input v-model="dataForm.patientCardno" placeholder="身份证号"></el-input>
    </el-form-item>
    <el-form-item label="饮酒习惯" prop="patientWine">
      <el-input v-model="dataForm.patientWine" placeholder="饮酒习惯"></el-input>
    </el-form-item>
    <el-form-item label="婚姻状态" prop="patientMarital">
      <el-input v-model="dataForm.patientMarital" placeholder="婚姻状态"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          patientId: 0,
          patientPackname: '',
          patientPwd: '',
          patientName: '',
          patientImg: '',
          patientBalance: '',
          patientIntegral: '',
          patientAddress: '',
          patientTel: '',
          patientBirth: '',
          patientAge: '',
          patientWeight: '',
          patientHight: '',
          patientShen: '',
          patientGan: '',
          patientFertility: '',
          patientHistory: '',
          patientNum: '',
          patientMoney: '',
          patientSex: '',
          patientCardno: '',
          patientWine: '',
          patientMarital: ''
        },
        dataRule: {
          patientPackname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          patientPwd: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          patientName: [
            { required: true, message: '患者姓名不能为空', trigger: 'blur' }
          ],
          patientImg: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          patientBalance: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ],
          patientIntegral: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          patientAddress: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          patientTel: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          patientBirth: [
            { required: true, message: '出生日期不能为空', trigger: 'blur' }
          ],
          patientAge: [
            { required: true, message: '年龄不能为空', trigger: 'blur' }
          ],
          patientWeight: [
            { required: true, message: '体重不能为空', trigger: 'blur' }
          ],
          patientHight: [
            { required: true, message: '身高不能为空', trigger: 'blur' }
          ],
          patientShen: [
            { required: true, message: '肾功能不能为空', trigger: 'blur' }
          ],
          patientGan: [
            { required: true, message: '肝功能不能为空', trigger: 'blur' }
          ],
          patientFertility: [
            { required: true, message: '生育状态不能为空', trigger: 'blur' }
          ],
          patientHistory: [
            { required: true, message: '疾病史不能为空', trigger: 'blur' }
          ],
          patientNum: [
            { required: true, message: '问诊次数不能为空', trigger: 'blur' }
          ],
          patientMoney: [
            { required: true, message: '支付总额不能为空', trigger: 'blur' }
          ],
          patientSex: [
            { required: true, message: '患者性别不能为空', trigger: 'blur' }
          ],
          patientCardno: [
            { required: true, message: '身份证号不能为空', trigger: 'blur' }
          ],
          patientWine: [
            { required: true, message: '饮酒习惯不能为空', trigger: 'blur' }
          ],
          patientMarital: [
            { required: true, message: '婚姻状态不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.patientId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.patientId) {
            this.$http({
              url: this.$http.adornUrl(`/medical/patient/info/${this.dataForm.patientId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.patientPackname = data.patient.patientPackname
                this.dataForm.patientPwd = data.patient.patientPwd
                this.dataForm.patientName = data.patient.patientName
                this.dataForm.patientImg = data.patient.patientImg
                this.dataForm.patientBalance = data.patient.patientBalance
                this.dataForm.patientIntegral = data.patient.patientIntegral
                this.dataForm.patientAddress = data.patient.patientAddress
                this.dataForm.patientTel = data.patient.patientTel
                this.dataForm.patientBirth = data.patient.patientBirth
                this.dataForm.patientAge = data.patient.patientAge
                this.dataForm.patientWeight = data.patient.patientWeight
                this.dataForm.patientHight = data.patient.patientHight
                this.dataForm.patientShen = data.patient.patientShen
                this.dataForm.patientGan = data.patient.patientGan
                this.dataForm.patientFertility = data.patient.patientFertility
                this.dataForm.patientHistory = data.patient.patientHistory
                this.dataForm.patientNum = data.patient.patientNum
                this.dataForm.patientMoney = data.patient.patientMoney
                this.dataForm.patientSex = data.patient.patientSex
                this.dataForm.patientCardno = data.patient.patientCardno
                this.dataForm.patientWine = data.patient.patientWine
                this.dataForm.patientMarital = data.patient.patientMarital
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/medical/patient/${!this.dataForm.patientId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'patientId': this.dataForm.patientId || undefined,
                'patientPackname': this.dataForm.patientPackname,
                'patientPwd': this.dataForm.patientPwd,
                'patientName': this.dataForm.patientName,
                'patientImg': this.dataForm.patientImg,
                'patientBalance': this.dataForm.patientBalance,
                'patientIntegral': this.dataForm.patientIntegral,
                'patientAddress': this.dataForm.patientAddress,
                'patientTel': this.dataForm.patientTel,
                'patientBirth': this.dataForm.patientBirth,
                'patientAge': this.dataForm.patientAge,
                'patientWeight': this.dataForm.patientWeight,
                'patientHight': this.dataForm.patientHight,
                'patientShen': this.dataForm.patientShen,
                'patientGan': this.dataForm.patientGan,
                'patientFertility': this.dataForm.patientFertility,
                'patientHistory': this.dataForm.patientHistory,
                'patientNum': this.dataForm.patientNum,
                'patientMoney': this.dataForm.patientMoney,
                'patientSex': this.dataForm.patientSex,
                'patientCardno': this.dataForm.patientCardno,
                'patientWine': this.dataForm.patientWine,
                'patientMarital': this.dataForm.patientMarital
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
